﻿using System.ComponentModel;
using System;
using System.Collections;
using System.IO;

using SourceGrid;
using SourceGrid.Cells;

namespace iPdf
{
    class FileInfoComparer : IComparer
    {
        int IComparer.Compare(object x, object y)
        {
            if (x == null && y == null)
                return 0;
            if (x == null)
                return 1;
            if (y == null)
                return -1;

            FileInfo file1 = x as FileInfo;
            FileInfo file2 = y as FileInfo;
            if (file1 == null || file2 == null)
            {
                throw new ArgumentException("Invalid arguments");
            }

            return file2.CreationTime.CompareTo(file1.CreationTime);
        }
    }


    class SessionsGridRowByNameComparer : IComparer
    {
        int IComparer.Compare(object x, object y)
        {
            if (x == null && y == null)
                return 0;
            if (x == null)
                return -1;
            if (y == null)
                return 1;

            Cell cell1 = x as Cell;
            Cell cell2 = y as Cell;
            if ( cell1 == null || cell2 == null )
            {
                throw new ArgumentException("Invalid arguments");
            }

            TataSession session1 = (TataSession)cell1.Tag;
            TataSession session2 = (TataSession)cell2.Tag;

            return session1.Name.CompareTo(session2.Name);
        }
    }

    class SessionsGridRowByTimeModifiedComparer : IComparer
    {
        int IComparer.Compare(object x, object y)
        {
            if (x == null && y == null)
                return 0;
            if (x == null)
                return -1;
            if (y == null)
                return 1;

            Cell cell1 = x as Cell;
            Cell cell2 = y as Cell;
            if (cell1 == null || cell2 == null)
            {
                throw new ArgumentException("Invalid arguments");
            }

            
            TataSession session1 = (TataSession)cell1.Tag;
            TataSession session2 = (TataSession)cell2.Tag;

            FileInfo file1 = new FileInfo(session1.FileName);
            FileInfo file2 = new FileInfo(session2.FileName);

            return file1.LastWriteTime.CompareTo(file2.LastWriteTime);
        }
    }

    class SessionsGridRowByPageComparer : IComparer
    {
        int IComparer.Compare(object x, object y)
        {
            if (x == null && y == null)
                return 0;
            if (x == null)
                return -1;
            if (y == null)
                return 1;

            Cell cell1 = x as Cell;
            Cell cell2 = y as Cell;
            if (cell1 == null || cell2 == null)
            {
                throw new ArgumentException("Invalid arguments");
            }

            TataSession session1 = (TataSession)cell1.Tag;
            TataSession session2 = (TataSession)cell2.Tag;

            return session1.Page.Name.CompareTo(session2.Page.Name);
        }
    }
}